package team1.hdhnewsapp.weatheractivity;

import info.androidhive.slidingmenu.R;

import java.util.List;

import team1.hdhnewsapp.weather.model.Weather;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<Weather> {
	Activity context;
	int layoutId;
	List<Weather> list;

	public MyArrayAdapter(Activity context, int layoutId,
			List<Weather> objects) {
		super(context, layoutId, objects);
		this.context = context;
		this.layoutId = layoutId;
		this.list = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = context.getLayoutInflater();
		convertView = inflater.inflate(layoutId, null);
		
		TextView cityText = (TextView) convertView.findViewById(R.id.cityText);
		TextView condDescr = (TextView) convertView.findViewById(R.id.condDescr);
		TextView temp = (TextView) convertView.findViewById(R.id.temp);
		TextView hum = (TextView) convertView.findViewById(R.id.hum);
		TextView press = (TextView) convertView.findViewById(R.id.press);
		TextView  windSpeed = (TextView) convertView.findViewById(R.id.windSpeed);
		TextView windDeg = (TextView) convertView.findViewById(R.id.windDeg);
		ImageView imgView = (ImageView) convertView.findViewById(R.id.condIcon);
		
		Weather weather = list.get(position);
		
		if (weather.iconData != null && weather.iconData.length > 0) {
			Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0,
					weather.iconData.length);
			imgView.setImageBitmap(img);
		}

		cityText.setText(weather.location.getCity() + ","
				+ weather.location.getCountry());
		condDescr.setText(weather.currentCondition.getCondition() + "("
				+ weather.currentCondition.getDescr() + "):");
		temp.setText(""
				+ Math.round((weather.temperature.getTemp() - 273.15))
				+ "oC");
		hum.setText(": " + weather.currentCondition.getHumidity() + "%");
		press.setText(": " + weather.currentCondition.getPressure() + " hPa");
		windSpeed.setText(": " + weather.wind.getSpeed() + " mps");
		windDeg.setText(": " + weather.wind.getDeg() + "o");

		return convertView;

	}

}
